package com.codelint.gt20post;

import com.tools.Byte2Hex;
import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: gzhang
 * Date: 5/27/13
 * Time: 8:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class GTHandler extends IoHandlerAdapter {

    Logger logger = App.logger;

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        cause.printStackTrace();
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        String str = message.toString();
        if (str.trim().equalsIgnoreCase("quit")) {
            session.close();
            return;
        }
        Date date = new Date();
        session.write(date.toString());

        logger.info("Message written: " + Byte2Hex.Bytes2HexString((byte[])message));
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        logger.info("IDLE " + session.getIdleCount(status));
    }
}
